package jsu.blogger;

import jsu.blogger.pojo.Draft;
import jsu.blogger.util.FileUtil;
import jsu.blogger.util.RegularUtil;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@MapperScan("jsu.blogger.mapper")
public class testFileUtil {
    @Test
    void testCreateDir(){

     boolean boo=FileUtil.createDirBySignId(FileUtil.POST_IMG_DIR_NAME,"1");
        System.out.println(boo);
    }

    @Test
    void getToRelativePath(){
        System.out.println(FileUtil.toRelativePath(FileUtil.BASE_PATH ));
    }

    @Test
    void testImg(){
        Map<String, Object> result = FileUtil.getImgAsBase64(FileUtil.PROFILE_PICTURE_DIR_NAME,String.valueOf(1));
        if(result.get("error")==null){
            System.out.println(result);
        }

    }

    @Test
    void getMaxFile(){
        String targetDirStr=FileUtil.createDirPath(FileUtil.TEMP_POST_IMG_DIR_NAME+File.separator+"123456_1_190");
        System.out.println(targetDirStr);
        File file=FileUtil.getMaxValueFileInDirectory(targetDirStr);
       // System.out.println(file.getName());
        if(file!=null) {
            System.out.println(file.getName());
        }
    }

    @Test
    void testRegular(){
        // 输入包含多个 img 标签的 HTML 字符串
        String htmlContent = "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1734273739310_1_964_1.jpg\"></p>"
                + "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1734273739311_2_965_2.jpg\"></p>"
                + "<p>哈哈哈哈</p>";

        // 调用方法提取所有的 storageFileName
        List<String> storageFileNames = RegularUtil.extractStorageFileNames(htmlContent);
        for (String fileNames:storageFileNames) {
            List<String> parts=RegularUtil.splitFileName(fileNames);
            System.out.println("前部分: " + parts.get(0) + ", 后部分: " + parts.get(1));
        }
        // 输出结果
        if (!storageFileNames.isEmpty()) {
            System.out.println("提取的 storageFileNames: " + storageFileNames);
        } else {
            System.out.println("未找到 storageFileName。");
        }
    }

    @Test
    void testContent(){
        String content = "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1734273739310_1_964_1.jpg\"></p>"
                + "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1_2.jpg\"></p>"+
                "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1_3.jpg\"></p>"+
                "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1_5.jpg\"></p>"+
                "<p><img src=\"\" data-local=\"blob:http://localhost:8080/89efa3d4-8fd5-4386-b75e-9919329a99e7\" data-custom=\"storageFileName=1_2.jpg\"></p>"+
                 "<p>哈哈哈哈</p>";
        long resourceId=1L;
        String draftId="1734273739310_1_964";
        //logger.info("内容是以谁为基础 " + content);
        List<String> storageFileNames = RegularUtil.extractStorageFileNames(content);
        Draft newDraft=new Draft();
        newDraft.setContent(content);

        for (String fileName : storageFileNames) {
            //正则表达式
            List<String> parts = RegularUtil.splitFileName(fileName);
            String frontPart = parts.get(0);  // 资源id
            String backPart = parts.get(1);//文件名
            String fileExtension = "";
            if (backPart!= null && backPart.lastIndexOf(".") > 0) {
                fileExtension = backPart.substring(backPart.lastIndexOf(".") + 1).toLowerCase();
            }
            if (frontPart.equals(String.valueOf(resourceId))) {
                //移动图片
                String sourcePathStr = FileUtil.createDirPath(FileUtil.POST_IMG_DIR_NAME + File.separator + frontPart + File.separator + backPart);
                Path sourcePath = Paths.get(sourcePathStr);  // 源文件路径
                String targetDirStr = FileUtil.createDirPath(FileUtil.TEMP_POST_IMG_DIR_NAME + File.separator + draftId);
                Path targetDir = Paths.get(targetDirStr);  // 目标目录路径
                File maxFile = FileUtil.getMaxValueFileInDirectory(targetDirStr);
                // System.out.println(file.getName());
                if (maxFile != null) {
                    String newFileName = String.valueOf(Integer.parseInt(Objects.requireNonNull(FileUtil.extractNumber(maxFile.getName()))) + 1);
                    newFileName=newFileName+"."+fileExtension;
                    boolean isCopy = FileUtil.copyFileAndRename(sourcePath, targetDir, newFileName);
                    if (isCopy) {
                        //修改content
                        newDraft.setContent(FileUtil.replaceStorageFileName(newDraft.getContent(), fileName, draftId + "_" + newFileName));
                    }
                }
            }
        }
        System.out.println("最后修改的内容: "+newDraft.getContent());
    }

}
